package com.atguigu.gmall.pms.service.impl;

import com.atguigu.gmall.pms.entity.*;
import com.atguigu.gmall.pms.mapper.AttrMapper;
import com.atguigu.gmall.pms.mapper.SkuAttrValueMapper;
import com.atguigu.gmall.pms.mapper.SpuAttrValueMapper;
import com.atguigu.gmall.pms.vo.AttrValueVo;
import com.atguigu.gmall.pms.vo.ItemGroupVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.annotation.Reference;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.atguigu.gmall.common.bean.PageResultVo;
import com.atguigu.gmall.common.bean.PageParamVo;

import com.atguigu.gmall.pms.mapper.AttrGroupMapper;
import com.atguigu.gmall.pms.service.AttrGroupService;
import org.springframework.util.CollectionUtils;


@Service("attrGroupService")
public class AttrGroupServiceImpl extends ServiceImpl<AttrGroupMapper, AttrGroupEntity> implements AttrGroupService {
    @Autowired
    private AttrMapper attrMapper;

    @Autowired
    private SkuAttrValueMapper saleAttrMapper;

    @Autowired
    private SpuAttrValueMapper baseAttrMapper;

    @Override
    public PageResultVo queryPage(PageParamVo paramVo) {
        IPage<AttrGroupEntity> page = this.page(
                paramVo.getPage(),
                new QueryWrapper<AttrGroupEntity>()
        );

        return new PageResultVo(page);
    }

    /**
     * 涉及多表联查   可使用关联查询(能不关联就不关联) 或 分步查询
     * 分步查询: 1. 查询所有的group信息
     * 2. 遍历group查询组下的规格参数
     *
     * @return
     */
    @Override
    public List<AttrGroupEntity> queryGroupsWithAttrsByCid(Long catId) {
        //1. 根据catId 查询分组
        QueryWrapper<AttrGroupEntity> wrapper = new QueryWrapper<>();
        wrapper.eq("category_id", catId);
        List<AttrGroupEntity> groupEntities = this.list(wrapper);

        //健壮性判断,如果为空则直接返回分组信息
        if (CollectionUtils.isEmpty(groupEntities)) {
            return groupEntities;
        }
        //2. 遍历分组查询每个组的规格参数   t ==> AttrGroupEntity
        //   根据分组Id 查询 组下规格参数并 只查询基础参数信息(type=1)
        groupEntities.forEach(attrGroupEntity -> {
            QueryWrapper<AttrEntity> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("group_id", attrGroupEntity.getId())
                    .eq("type", 1);
            List<AttrEntity> attrEntities = this.attrMapper.selectList(queryWrapper);
            attrGroupEntity.setAttrEntities(attrEntities);
        });

        return groupEntities;
    }

    @Override
    public List<ItemGroupVo> queryGroupWithAttrValuesByCidAndSpuIdAndSkuId(Long cid, Long spuId, Long skuId) {
        //根据cid查询当前分类下的规格参数组
        List<AttrGroupEntity> groupEntities = this.list(new QueryWrapper<AttrGroupEntity>().eq("category_id", cid));
        if (CollectionUtils.isEmpty(groupEntities)) {
            return null;
        }

        //把规格参数分组集合,转化成分组的 vo集合
        List<ItemGroupVo> groupVos = groupEntities.stream().map(groupEntity -> {
            ItemGroupVo itemGroupVo = new ItemGroupVo();

            itemGroupVo.setName(groupEntity.getName());
            //根据分组id查询组下的规格参数
            List<AttrEntity> attrEntities = attrMapper.selectList(new QueryWrapper<AttrEntity>().eq("group_id", groupEntity.getId()));

            if (!CollectionUtils.isEmpty(attrEntities)) {

                //获取分组下规格参数的id集合
                List<Long> attrIds = attrEntities.stream().map(AttrEntity::getId).collect(Collectors.toList());

                //private List<AttrValueVo> attrs; 需要设置的是一个list集合
                List<AttrValueVo> attrValueVos = new ArrayList<>();

                //查询销售类型的规格参数和值
                List<SkuAttrValueEntity> skuAttrValueEntities =
                        saleAttrMapper.selectList(new QueryWrapper<SkuAttrValueEntity>().eq("sku_id", skuId).in("attr_id", attrIds));
                //如果结果集不为空,将其转换成AttrValueVo 集合并保存到list内
                if (!CollectionUtils.isEmpty(skuAttrValueEntities)) {
                    attrValueVos.addAll(skuAttrValueEntities.stream().map(skuAttrValueEntity -> {
                        AttrValueVo attrValueVo = new AttrValueVo();
                        BeanUtils.copyProperties(skuAttrValueEntity, attrValueVo);
                        return attrValueVo;
                    }).collect(Collectors.toList()));
                }

                //查询基本类型的规格参数和值
                List<SpuAttrValueEntity> spuAttrValueEntities =
                        baseAttrMapper.selectList(new QueryWrapper<SpuAttrValueEntity>().eq("spu_id", spuId).in("attr_id", attrIds));
                //如果结果集不为空,将其转换成AttrValueVo 集合并保存到list内
                if (!CollectionUtils.isEmpty(spuAttrValueEntities)) {
                    attrValueVos.addAll(spuAttrValueEntities.stream().map(spuAttrValueEntity -> {
                        AttrValueVo attrValueVo = new AttrValueVo();
                        BeanUtils.copyProperties(spuAttrValueEntity, attrValueVo);
                        return attrValueVo;
                    }).collect(Collectors.toList()));
                }

                itemGroupVo.setAttrs(attrValueVos);
            }
            return itemGroupVo;
        }).collect(Collectors.toList());

        return groupVos;
    }
}